import { indexApi } from '../../../api/index';
import { request } from '../../../utils/request';

Page({
  data: {
    defaultData: {
      title: "订单进度", // 导航栏标题
      imageSrc: "/images/back.png",
      isBackToPreviousPage: true
    },
    orderId: null,
    orderList: [],
    order: null,
    steps: [
      { label: '发布订单', icon: 'icon-dingdan', isLastStep: false, color: '#8A8A8A', lineColor: '#8A8A8A' },
      { label: '等待抢单', icon: 'icon-qiangdan', isLastStep: false, color: '#8A8A8A', lineColor: '#8A8A8A' },
      { label: '预约时间', icon: 'icon-shijian', isLastStep: false, color: '#8A8A8A', lineColor: '#8A8A8A' },
      { label: '服务开始', icon: 'icon-gongju', isLastStep: false, color: '#8A8A8A', lineColor: '#8A8A8A' },
      { label: '验收评价', icon: 'icon-pingjia', isLastStep: true, color: '#8A8A8A', lineColor: '#8A8A8A' },
    ],
    lists: {
      generate: "2024-05-01 16:15",
      submit: "2024-05-01 16:15",
      payment: "2024-05-01 16:15",
      takingOrders: "等待",
      service: "等待",
      complete: "等待",
      acceptance: "等待",
      orderCompleted: "等待",
    }
  },

  onLoad(options) {
    console.log("options", options);
    this.setData({
      orderId: options.orderId
    });
    this.getOrderList();
  },

  //查询订单
  async getOrderList() {
    try {
      const res = await request({
        url: indexApi.ordeApi(),
        data: {
          pageNum: 0,
          pageSize: 10,
        }
      });
      console.log(res.data.list);
      this.setData({
        orderList: res.data.list
      });
      this.getOrder();
    } catch (error) {
      console.error("Error:", error);
    }
  },

  // 根据id查询订单
  getOrder() {
    const { orderId, orderList } = this.data;
    const order = orderList.find(order => order.orderId == orderId);
    if (order) {
      console.log("order:", order);
      this.updateSteps(order.status);
      this.setData({
        order: order
      });
    } else {
      console.log("Order not found");
    }
  },

  updateSteps(status) {
    const updatedSteps = this.data.steps.map((step, index) => {
      if (
        (status === 1 && index === 0) ||
        (status === 2 && index <= 1) ||
        (status === 3 && index <= 1) ||
        (status === 4 && index <= 2) ||
        (status === 5 && index <= 3) ||
        (status === 6 && index <= 4) ||
        (status === 7 && index <= 4)
      ) {
        step.color = '#2074F4';
      } else {
        step.color = '#8A8A8A';
      }

      if (index < status - 1) {
        step.lineColor = '#2074F4';
      } else {
        step.lineColor = '#8A8A8A';
      }

      return step;
    });

    this.setData({ steps: updatedSteps });
  },

  onShareAppMessage() {
    return {};
  },

  handleBlack() {
    wx.navigateBack({
      delta: 1
    });
  }
});
